var moment = require('moment');
var _ = require('underscore');

module.exports = function (app) {
  app.get('/mobile/user/:userId/app/:appId/activitytracesimilar.json', function (req, res) {
    var classify = req.query.classify;
    var size = parseInt(req.query.size);
    var limit = parseInt(req.query.limit);
    var offset = parseInt(req.query.offset);

    var result = null;

    if (size === 30) {
      result = {
        "result": [
          {
            "combData": {
              "affected": 4,
              "count": 6,
              "sm": 1,
              "et": 1446627600000,
              "executeTime": 40138,
              "firstTime": 1446024286000,
              "lastTime": 1446432449000,
              "st": 1446019200000
            },
            "data": [
              {
                "affected": 1,
                "count": 2,
                "sm": 11,
                "et": 1446048000000,
                "executeTime": 10027,
                "firstTime": 1446024286000,
                "lastTime": 1446024286000,
                "st": 1446019200000
              },
              {
                "affected": 0,
                "count": 0,
                "sm": 15,
                "et": 1446069600000,
                "executeTime": 0,
                "firstTime": 0,
                "lastTime": 0,
                "st": 1446048000000
              },
              {
                "affected": 1,
                "count": 1,
                "sm": 2,
                "et": 1446091200000,
                "executeTime": 10049,
                "firstTime": 1446089338000,
                "lastTime": 1446089338000,
                "st": 1446069600000
              },
              {
                "affected": 0,
                "count": 0,
                "sm": 8,
                "et": 1446112800000,
                "executeTime": 0,
                "firstTime": 0,
                "lastTime": 0,
                "st": 1446091200000
              },
              {
                "affected": 0,
                "count": 0,
                "sm": 22,
                "et": 1446134400000,
                "executeTime": 0,
                "firstTime": 0,
                "lastTime": 0,
                "st": 1446112800000
              },
              {
                "affected": 0,
                "count": 0,
                "sm": 23,
                "et": 1446156000000,
                "executeTime": 0,
                "firstTime": 0,
                "lastTime": 0,
                "st": 1446134400000
              },
              {
                "affected": 1,
                "count": 2,
                "sm": 9,
                "et": 1446177600000,
                "executeTime": 10032,
                "firstTime": 1446166246000,
                "lastTime": 1446166492000,
                "st": 1446156000000
              },
              {
                "affected": 0,
                "count": 0,
                "sm": 14,
                "et": 1446199200000,
                "executeTime": 0,
                "firstTime": 0,
                "lastTime": 0,
                "st": 1446177600000
              },
              {
                "affected": 0,
                "count": 0,
                "sm": 8,
                "et": 1446220800000,
                "executeTime": 0,
                "firstTime": 0,
                "lastTime": 0,
                "st": 1446199200000
              },
              {
                "affected": 0,
                "count": 0,
                "sm": 4,
                "et": 1446242400000,
                "executeTime": 0,
                "firstTime": 0,
                "lastTime": 0,
                "st": 1446220800000
              },
              {
                "affected": 0,
                "count": 0,
                "sm": 9,
                "et": 1446264000000,
                "executeTime": 0,
                "firstTime": 0,
                "lastTime": 0,
                "st": 1446242400000
              },
              {
                "affected": 0,
                "count": 0,
                "sm": 10,
                "et": 1446285600000,
                "executeTime": 0,
                "firstTime": 0,
                "lastTime": 0,
                "st": 1446264000000
              },
              {
                "affected": 0,
                "count": 0,
                "sm": 7,
                "et": 1446307200000,
                "executeTime": 0,
                "firstTime": 0,
                "lastTime": 0,
                "st": 1446285600000
              },
              {
                "affected": 0,
                "count": 0,
                "sm": 8,
                "et": 1446328800000,
                "executeTime": 0,
                "firstTime": 0,
                "lastTime": 0,
                "st": 1446307200000
              },
              {
                "affected": 0,
                "count": 0,
                "sm": 5,
                "et": 1446350400000,
                "executeTime": 0,
                "firstTime": 0,
                "lastTime": 0,
                "st": 1446328800000
              },
              {
                "affected": 0,
                "count": 0,
                "sm": 17,
                "et": 1446372000000,
                "executeTime": 0,
                "firstTime": 0,
                "lastTime": 0,
                "st": 1446350400000
              },
              {
                "affected": 0,
                "count": 0,
                "sm": 6,
                "et": 1446393600000,
                "executeTime": 0,
                "firstTime": 0,
                "lastTime": 0,
                "st": 1446372000000
              },
              {
                "affected": 0,
                "count": 0,
                "sm": 5,
                "et": 1446415200000,
                "executeTime": 0,
                "firstTime": 0,
                "lastTime": 0,
                "st": 1446393600000
              },
              {
                "affected": 1,
                "count": 1,
                "sm": 4,
                "et": 1446436800000,
                "executeTime": 10030,
                "firstTime": 1446432449000,
                "lastTime": 1446432449000,
                "st": 1446415200000
              },
              {
                "affected": 0,
                "count": 0,
                "sm": 1,
                "et": 1446458400000,
                "executeTime": 0,
                "firstTime": 0,
                "lastTime": 0,
                "st": 1446436800000
              },
              {
                "affected": 0,
                "count": 0,
                "sm": 20,
                "et": 1446480000000,
                "executeTime": 0,
                "firstTime": 0,
                "lastTime": 0,
                "st": 1446458400000
              },
              {
                "affected": 0,
                "count": 0,
                "sm": 9,
                "et": 1446501600000,
                "executeTime": 0,
                "firstTime": 0,
                "lastTime": 0,
                "st": 1446480000000
              },
              {
                "affected": 0,
                "count": 0,
                "sm": 4,
                "et": 1446523200000,
                "executeTime": 0,
                "firstTime": 0,
                "lastTime": 0,
                "st": 1446501600000
              },
              {
                "affected": 0,
                "count": 0,
                "sm": 5,
                "et": 1446544800000,
                "executeTime": 0,
                "firstTime": 0,
                "lastTime": 0,
                "st": 1446523200000
              },
              {
                "affected": 0,
                "count": 0,
                "sm": 7,
                "et": 1446566400000,
                "executeTime": 0,
                "firstTime": 0,
                "lastTime": 0,
                "st": 1446544800000
              },
              {
                "affected": 0,
                "count": 0,
                "sm": 3,
                "et": 1446588000000,
                "executeTime": 0,
                "firstTime": 0,
                "lastTime": 0,
                "st": 1446566400000
              },
              {
                "affected": 0,
                "count": 0,
                "sm": 8,
                "et": 1446609600000,
                "executeTime": 0,
                "firstTime": 0,
                "lastTime": 0,
                "st": 1446588000000
              },
              {
                "affected": 0,
                "count": 0,
                "sm": 19,
                "et": 1446627600000,
                "executeTime": 0,
                "firstTime": 0,
                "lastTime": 0,
                "st": 1446609600000
              }
            ],
            "key": {
              "activityName": "TestWebView2#onCreate",
              "appVersion": "1.7.6.6",
              "appVersionId": 5593,
              "id": 5593,
              "name": "1.7.6.6",
              "nameId": 485837,
              "osVersion": "4.4.2",
              "osVersionId": 15
            }
          }
        ],
        "timeSpan": {
          "endTime": 1446627600000,
          "startTime": 1446019200000
        }
      };
    } else if (!isNaN(limit)) {
      var data = [];
      for (var i = 0; i < 100; i++) {
        var item = {
          key: {
            nameId: i,
            name: 'name-' + i
          },
          data: [{
            avg: 29,
            sm: 25,
            count: 45,
            lastTime: moment().subtract(i, 'minutes').valueOf()
          }]
        };
        data.push(item);
      }

      if(!isNaN(offset) && offset) {
        data = _.filter(data, function(item, index) {return index >= offset});
      }

      if (limit) {
        data = _.filter(data, function(item, index) {return index < limit});
      }

      result = { result: data };
    } else {
      result = {
        "result": [
          {
            "combData": {
              "affected": 1,
              "count": 2,
              "sm": 12,
              "et": 1446627600000,
              "executeTime": 10027,
              "firstTime": 1446024286000,
              "lastTime": 1446166492000,
              "st": 1446019200000
            },
            "data": [
              {
                "affected": 1,
                "count": 2,
                "sm": 19,
                "et": 1446627600000,
                "executeTime": 10027,
                "firstTime": 1446024286000,
                "lastTime": 1446166492000,
                "st": 1446019200000
              }
            ],
            "key": {
              "activityName": "TestWebView2#onCreate",
              "appVersion": "1.7.6.6",
              "appVersionId": 5593,
              "id": 15,
              "name": "4.4.2",
              "nameId": 485837,
              "osVersion": "4.4.2",
              "osVersionId": 15
            }
          }, {
            "combData": {
              "affected": 1,
              "count": 2,
              "sm": 12,
              "et": 1446627600000,
              "executeTime": 10027,
              "firstTime": 1446024286000,
              "lastTime": 1446166492000,
              "st": 1446019200000
            },
            "data": [
              {
                "affected": 1,
                "count": 2,
                "sm": 19,
                "et": 1446627600000,
                "executeTime": 10027,
                "firstTime": 1446024286000,
                "lastTime": 1446166492000,
                "st": 1446019200000
              }
            ],
            "key": {
              "activityName": "TestWebView3#onUpdate",
              "appVersion": "1.7.6.6",
              "appVersionId": 5593,
              "id": 15,
              "name": "4.4.2",
              "nameId": 485837,
              "osVersion": "4.4.2",
              "osVersionId": 15
            }
          }
        ],
        "timeSpan": {
          "endTime": 1446627600000,
          "startTime": 1446019200000
        }
      };
    }

    res.status(200).json(result);
  });

  app.get('/mobile/user/:userId/app/:appId/activitytracelist.json', function (req, res) {
    res.status(200).json([
      {
        "appId": 169,
        "appVersionId": 225,
        "appVersionName": "2.0.2",
        "deviceId": 0,
        "deviceType": "unknown",
        "deviceTypeId": 0,
        "executeTime": 782,
        "id": 1327,
        "name": "MainActivity#onCreate",
        "nameId": 0,
        "osId": 1,
        "osName": "Android",
        "osVersionId": 9,
        "osVersionName": "4.2.1",
        "sm": 0,
        "timestamp": 1447048320000,
        "cpu": [
          [
            1439890574673,
            0
          ],
          [
            1439890574772,
            0
          ]
        ]
      },
      {
        "appId": 169,
        "appVersionId": 170,
        "appVersionName": "1.2.1",
        "deviceId": 0,
        "deviceType": "unknown",
        "deviceTypeId": 0,
        "executeTime": 3491,
        "id": 1312,
        "name": "MainActivity#onCreate",
        "nameId": 0,
        "osId": 1,
        "osName": "Android",
        "osVersionId": 2,
        "osVersionName": "4.1.2",
        "sm": 0,
        "timestamp": 1446449060000,
        "cpu": [
          [
            1439890574673,
            0.9
          ],
          [
            1439890574772,
            0.4
          ],
          [
            1439890574772,
            0.2
          ],
          [
            1439890574772,
            0.6
          ]
        ]
      },
      {
        "appId": 169,
        "appVersionId": 170,
        "appVersionName": "1.2.1",
        "deviceId": 0,
        "deviceType": "unknown",
        "deviceTypeId": 0,
        "executeTime": 1453,
        "id": 1311,
        "name": "MainActivity#onCreate",
        "nameId": 0,
        "osId": 1,
        "osName": "Android",
        "osVersionId": 2,
        "osVersionName": "4.1.2",
        "sm": 0,
        "timestamp": 1446446242000,
        "cpu": [
          [
            1439890574673,
            0.5
          ],
          [
            1439890574772,
            0.7
          ],
          [
            1439890574772,
            0.8
          ],
          [
            1439890574772,
            0.6
          ]
        ]
      }
    ]);
  });
}
